<template>
  <div class="min-h-screen bg-gradient-to-br from-blue-50 via-indigo-50 to-purple-50">
    <div class="container mx-auto px-4 py-8">
      <!-- 页面标题 -->
      <div class="mb-8 animate-fade-in">
        <div class="flex justify-between items-center">
          <div class="flex items-center space-x-4">
            <div class="p-3 bg-gradient-to-r from-blue-600 to-purple-600 rounded-2xl shadow-lg">
              <svg class="w-8 h-8 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4M7.835 4.697a3.42 3.42 0 001.946-.806 3.42 3.42 0 014.438 0 3.42 3.42 0 001.946.806 3.42 3.42 0 013.138 3.138 3.42 3.42 0 00.806 1.946 3.42 3.42 0 010 4.438 3.42 3.42 0 00-.806 1.946 3.42 3.42 0 01-3.138 3.138 3.42 3.42 0 00-1.946.806 3.42 3.42 0 01-4.438 0 3.42 3.42 0 00-1.946-.806 3.42 3.42 0 01-3.138-3.138 3.42 3.42 0 00-.806-1.946 3.42 3.42 0 010-4.438 3.42 3.42 0 00.806-1.946 3.42 3.42 0 013.138-3.138z"></path>
              </svg>
            </div>
            <div>
              <h1 class="text-4xl font-bold bg-gradient-to-r from-gray-900 to-gray-700 bg-clip-text text-transparent">竞赛管理</h1>
              <p class="text-gray-600 mt-1">管理系统竞赛和比赛</p>
            </div>
          </div>
          <button
            class="px-6 py-3 bg-gradient-to-r from-blue-600 to-purple-600 text-white font-semibold rounded-xl hover:from-blue-700 hover:to-purple-700 transition-all duration-200 hover:scale-105 shadow-lg"
            @click="createContest"
          >
            <svg class="w-5 h-5 inline mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6"></path>
            </svg>
            创建竞赛
          </button>
        </div>
      </div>

      <!-- 搜索和筛选 -->
      <div class="bg-white/80 backdrop-blur-sm rounded-2xl shadow-lg p-6 mb-8 animate-slide-up">
        <div class="grid grid-cols-1 md:grid-cols-4 gap-6">
          <div class="md:col-span-2">
            <div class="relative">
              <input
                v-model="searchQuery"
                type="text"
                placeholder="搜索竞赛..."
                class="w-full pl-12 pr-4 py-3 bg-white/70 border border-gray-200 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all duration-200 placeholder-gray-400"
                @input="handleSearch"
              />
              <svg class="absolute left-4 top-3.5 h-5 w-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path>
              </svg>
            </div>
          </div>

          <div>
            <select
              v-model="selectedStatus"
              class="w-full px-4 py-3 bg-white/70 border border-gray-200 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all duration-200"
              @change="handleFilter"
            >
              <option value="">所有状态</option>
              <option value="upcoming">即将开始</option>
              <option value="ongoing">进行中</option>
              <option value="finished">已结束</option>
            </select>
          </div>

          <div>
            <select
              v-model="selectedType"
              class="w-full px-4 py-3 bg-white/70 border border-gray-200 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all duration-200"
              @change="handleFilter"
            >
              <option value="">所有类型</option>
              <option value="weekly">周赛</option>
              <option value="biweekly">双周赛</option>
              <option value="monthly">月赛</option>
              <option value="special">特殊竞赛</option>
            </select>
          </div>
        </div>
      </div>

      <!-- 竞赛列表 -->
      <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 animate-slide-up animation-delay-200">
        <div
          v-for="contest in filteredContests"
          :key="contest.id"
          class="bg-white/80 backdrop-blur-sm rounded-2xl shadow-lg hover:shadow-xl transition-all duration-300 hover:scale-[1.02] p-6"
        >
          <!-- 竞赛标题和描述 -->
          <div class="mb-4">
            <h3 class="text-lg font-semibold text-gray-900 mb-2">{{ contest.title }}</h3>
            <p class="text-sm text-gray-600 line-clamp-2">{{ contest.description }}</p>
          </div>

          <!-- 类型和状态标签 -->
          <div class="flex space-x-2 mb-4">
            <span class="px-3 py-1 bg-gradient-to-r from-blue-100 to-blue-200 text-blue-800 text-xs rounded-full font-semibold">
              {{ contest.type }}
            </span>
            <span
              :class="[
                'px-3 py-1 rounded-full text-xs font-semibold',
                contest.status === 'upcoming' ? 'bg-gradient-to-r from-blue-100 to-blue-200 text-blue-800' :
                contest.status === 'running' ? 'bg-gradient-to-r from-green-100 to-green-200 text-green-800' :
                'bg-gradient-to-r from-gray-100 to-gray-200 text-gray-800'
              ]"
            >
              {{ contest.status === 'upcoming' ? '即将开始' : contest.status === 'running' ? '进行中' : '已结束' }}
            </span>
          </div>

          <!-- 竞赛信息 -->
          <div class="space-y-3 mb-6">
            <div class="flex items-center text-sm text-gray-600">
              <svg class="w-4 h-4 mr-2 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197m13.5-9a2.5 2.5 0 11-5 0 2.5 2.5 0 015 0z"></path>
              </svg>
              参赛人数：<span class="font-semibold text-blue-600">{{ contest.participant_count }}</span>
            </div>
            <div class="flex items-center text-sm text-gray-600">
              <svg class="w-4 h-4 mr-2 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path>
              </svg>
              开始时间：{{ formatDateTime(contest.start_time) }}
            </div>
          </div>

          <!-- 操作按钮 -->
          <div class="flex flex-wrap gap-2">
            <button
              class="px-3 py-2 bg-gradient-to-r from-blue-500 to-blue-600 text-white text-sm font-medium rounded-xl hover:from-blue-600 hover:to-blue-700 transition-all duration-200 hover:scale-105"
              @click="editContest(contest)"
            >
              编辑
            </button>
            <button
              class="px-3 py-2 bg-gradient-to-r from-green-500 to-green-600 text-white text-sm font-medium rounded-xl hover:from-green-600 hover:to-green-700 transition-all duration-200 hover:scale-105"
              @click="viewContest(contest)"
            >
              查看
            </button>
            <button
              v-if="contest.status === 'upcoming'"
              class="px-3 py-2 bg-gradient-to-r from-emerald-500 to-emerald-600 text-white text-sm font-medium rounded-xl hover:from-emerald-600 hover:to-emerald-700 transition-all duration-200 hover:scale-105"
              @click="() => console.log('开始竞赛:', contest)"
            >
              开始
            </button>
            <button
              v-if="contest.status === 'running'"
              class="px-3 py-2 bg-gradient-to-r from-red-500 to-red-600 text-white text-sm font-medium rounded-xl hover:from-red-600 hover:to-red-700 transition-all duration-200 hover:scale-105"
              @click="() => console.log('结束竞赛:', contest)"
            >
              结束
            </button>
            <button
              class="px-3 py-2 bg-gradient-to-r from-gray-500 to-gray-600 text-white text-sm font-medium rounded-xl hover:from-gray-600 hover:to-gray-700 transition-all duration-200 hover:scale-105"
              @click="deleteContest(contest)"
            >
              <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path>
              </svg>
            </button>
          </div>
        </div>
      </div>

      <!-- 空状态 -->
      <div v-if="filteredContests.length === 0" class="text-center py-12 animate-fade-in">
        <div class="mx-auto w-24 h-24 bg-gradient-to-r from-gray-100 to-gray-200 rounded-full flex items-center justify-center mb-4">
          <svg class="w-12 h-12 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4M7.835 4.697a3.42 3.42 0 001.946-.806 3.42 3.42 0 014.438 0 3.42 3.42 0 001.946.806 3.42 3.42 0 013.138 3.138 3.42 3.42 0 00.806 1.946 3.42 3.42 0 010 4.438 3.42 3.42 0 00-.806 1.946 3.42 3.42 0 01-3.138 3.138 3.42 3.42 0 00-1.946.806 3.42 3.42 0 01-4.438 0 3.42 3.42 0 00-1.946-.806 3.42 3.42 0 01-3.138-3.138 3.42 3.42 0 00-.806-1.946 3.42 3.42 0 010-4.438 3.42 3.42 0 00.806-1.946 3.42 3.42 0 013.138-3.138z"></path>
          </svg>
        </div>
        <h3 class="text-lg font-semibold text-gray-900 mb-2">暂无竞赛</h3>
        <p class="text-gray-500">没有找到符合条件的竞赛</p>
      </div>

      <!-- 分页 -->
      <div v-if="totalPages > 1" class="mt-8 animate-fade-in">
        <div class="bg-white/80 backdrop-blur-sm rounded-2xl shadow-lg p-6">
          <div class="flex items-center justify-between">
            <div class="flex-1 flex justify-between sm:hidden">
              <button
                :disabled="currentPage === 1"
                class="relative inline-flex items-center px-4 py-2 bg-gradient-to-r from-blue-500 to-blue-600 text-white text-sm font-medium rounded-xl hover:from-blue-600 hover:to-blue-700 disabled:opacity-50 disabled:cursor-not-allowed transition-all duration-200"
                @click="goToPage(currentPage - 1)"
              >
                <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"></path>
                </svg>
                上一页
              </button>
              <button
                :disabled="currentPage === totalPages"
                class="ml-3 relative inline-flex items-center px-4 py-2 bg-gradient-to-r from-blue-500 to-blue-600 text-white text-sm font-medium rounded-xl hover:from-blue-600 hover:to-blue-700 disabled:opacity-50 disabled:cursor-not-allowed transition-all duration-200"
                @click="goToPage(currentPage + 1)"
              >
                下一页
                <svg class="w-4 h-4 ml-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path>
                </svg>
              </button>
            </div>
            <div class="hidden sm:flex-1 sm:flex sm:items-center sm:justify-between">
              <div>
                <p class="text-sm text-gray-700">
                  显示第
                  <span class="font-semibold text-blue-600">{{ (currentPage - 1) * pageSize + 1 }}</span>
                  到
                  <span class="font-semibold text-blue-600">{{ Math.min(currentPage * pageSize, totalContests) }}</span>
                  条，共
                  <span class="font-semibold text-blue-600">{{ totalContests }}</span>
                  条记录
                </p>
              </div>
              <div>
                <nav class="relative z-0 inline-flex rounded-xl shadow-sm -space-x-px">
                  <button
                    :disabled="currentPage === 1"
                    class="relative inline-flex items-center px-3 py-2 rounded-l-xl border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed transition-all duration-200"
                    @click="goToPage(currentPage - 1)"
                  >
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"></path>
                    </svg>
                  </button>
                  <button
                    v-for="page in visiblePages"
                    :key="page"
                    :class="[
                      'relative inline-flex items-center px-4 py-2 border text-sm font-medium transition-all duration-200',
                      page === currentPage
                        ? 'z-10 bg-gradient-to-r from-blue-500 to-blue-600 border-blue-500 text-white shadow-lg'
                        : 'bg-white border-gray-300 text-gray-500 hover:bg-gray-50'
                    ]"
                    @click="goToPage(page)"
                  >
                    {{ page }}
                  </button>
                  <button
                    :disabled="currentPage === totalPages"
                    class="relative inline-flex items-center px-3 py-2 rounded-r-xl border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed transition-all duration-200"
                    @click="goToPage(currentPage + 1)"
                  >
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path>
                    </svg>
                  </button>
                </nav>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 创建竞赛模态框 -->
    <div v-if="showCreateModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
      <div class="bg-white rounded-2xl shadow-2xl w-full max-w-2xl mx-4 max-h-[90vh] overflow-y-auto">
        <div class="p-6 border-b border-gray-200">
          <div class="flex items-center justify-between">
            <h3 class="text-xl font-semibold text-gray-900">创建新竞赛</h3>
            <button 
              @click="showCreateModal = false"
              class="text-gray-400 hover:text-gray-600 transition-colors"
            >
              <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
              </svg>
            </button>
          </div>
        </div>

        <form @submit.prevent="submitCreateContest" class="p-6 space-y-6">
          <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">竞赛标题 *</label>
              <input
                v-model="createForm.title"
                type="text"
                required
                class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="请输入竞赛标题"
              />
            </div>

            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">竞赛类型 *</label>
              <select
                v-model="createForm.type"
                required
                class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
              >
                <option value="individual">个人竞赛</option>
                <option value="team">团队竞赛</option>
              </select>
            </div>
          </div>

          <div>
            <label class="block text-sm font-medium text-gray-700 mb-2">竞赛描述</label>
            <textarea
              v-model="createForm.description"
              rows="3"
              class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
              placeholder="请输入竞赛描述"
            ></textarea>
          </div>

          <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">开始时间 *</label>
              <input
                v-model="createForm.start_time"
                type="datetime-local"
                required
                class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
              />
            </div>

            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">结束时间 *</label>
              <input
                v-model="createForm.end_time"
                type="datetime-local"
                required
                class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
              />
            </div>
          </div>

          <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">竞赛时长（分钟） *</label>
              <input
                v-model.number="createForm.duration"
                type="number"
                required
                min="1"
                class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="90"
              />
            </div>

            <div>
              <label class="block text-sm font-medium text-gray-700 mb-2">最大参赛人数</label>
              <input
                v-model.number="createForm.max_participants"
                type="number"
                min="1"
                class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                placeholder="不限制"
              />
            </div>
          </div>

          <div>
            <label class="block text-sm font-medium text-gray-700 mb-2">竞赛规则</label>
            <textarea
              v-model="createForm.rules"
              rows="4"
              class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
              placeholder="请输入竞赛规则和说明"
            ></textarea>
          </div>

          <div class="flex items-center">
            <input
              v-model="createForm.is_public"
              type="checkbox"
              class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded"
            />
            <label class="ml-2 block text-sm text-gray-700">
              公开竞赛（所有用户可见）
            </label>
          </div>

          <div class="flex justify-end space-x-4 pt-6 border-t border-gray-200">
            <button
              type="button"
              @click="showCreateModal = false"
              class="px-6 py-3 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition-colors"
            >
              取消
            </button>
            <button
              type="submit"
              :disabled="loading"
              class="px-6 py-3 bg-blue-600 text-white rounded-lg hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
            >
              {{ loading ? '创建中...' : '创建竞赛' }}
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { adminApi } from '@/api/admin'

const router = useRouter()

// 响应式数据
const searchQuery = ref('')
const selectedStatus = ref('')
const selectedType = ref('')
const currentPage = ref(1)
const pageSize = ref(20)
const loading = ref(false)
const total = ref(0)

// 竞赛数据
const contests = ref<Array<{
  id: number
  title: string
  description?: string
  type: 'individual' | 'team'
  status: 'upcoming' | 'running' | 'ended'
  start_time: string
  end_time: string
  duration: number
  max_participants?: number
  participant_count: number
  is_public: boolean
  author_id: number
  author?: {
    id: number
    username: string
    avatar?: string
  }
  created_at: string
  updated_at: string
}>>([])

// 计算属性
const filteredContests = computed(() => {
  let filtered = contests.value

  // 搜索过滤
  if (searchQuery.value) {
    const query = searchQuery.value.toLowerCase()
    filtered = filtered.filter(c =>
      c.title.toLowerCase().includes(query) ||
      (c.description && c.description.toLowerCase().includes(query))
    )
  }

  // 状态过滤
  if (selectedStatus.value) {
    filtered = filtered.filter(c => c.status === selectedStatus.value)
  }

  // 类型过滤
  if (selectedType.value) {
    filtered = filtered.filter(c => c.type === selectedType.value)
  }

  return filtered
})

const totalContests = computed(() => filteredContests.value.length)
const totalPages = computed(() => Math.ceil(totalContests.value / pageSize.value))

const visiblePages = computed(() => {
  const pages = []
  const start = Math.max(1, currentPage.value - 2)
  const end = Math.min(totalPages.value, currentPage.value + 2)
  
  for (let i = start; i <= end; i++) {
    pages.push(i)
  }
  
  return pages
})

// 数据加载方法
const loadContests = async () => {
  try {
    loading.value = true
    const response = await adminApi.getContests({
      status: selectedStatus.value || undefined,
      type: selectedType.value || undefined,
      search: searchQuery.value || undefined,
      page: currentPage.value,
      page_size: pageSize.value
    })
    
    if (response.data && Array.isArray(response.data.contests)) {
      contests.value = response.data.contests
      total.value = response.data.total
    } else {
      contests.value = []
      total.value = 0
    }
  } catch (error) {
    console.error('加载竞赛数据失败:', error)
    contests.value = []
    total.value = 0
  } finally {
    loading.value = false
  }
}

// 方法
const handleSearch = () => {
  currentPage.value = 1
  loadContests()
}

const handleFilter = () => {
  currentPage.value = 1
  loadContests()
}

const goToPage = (page: number) => {
  if (page >= 1 && page <= totalPages.value) {
    currentPage.value = page
    loadContests()
  }
}

const formatDateTime = (dateString: string) => {
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 创建竞赛相关
const showCreateModal = ref(false)
const createForm = ref({
  title: '',
  description: '',
  type: 'individual' as 'individual' | 'team',
  start_time: '',
  end_time: '',
  duration: 90,
  max_participants: undefined as number | undefined,
  is_public: true,
  rules: ''
})

const createContest = () => {
  showCreateModal.value = true
}

const submitCreateContest = async () => {
  try {
    loading.value = true
    
    // 转换时间格式：从 "2024-01-15T10:00" 转换为 "2006-01-02 15:04:05"
    const formatDateTime = (dateTimeString: string) => {
      const date = new Date(dateTimeString)
      const year = date.getFullYear()
      const month = String(date.getMonth() + 1).padStart(2, '0')
      const day = String(date.getDate()).padStart(2, '0')
      const hours = String(date.getHours()).padStart(2, '0')
      const minutes = String(date.getMinutes()).padStart(2, '0')
      const seconds = String(date.getSeconds()).padStart(2, '0')
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
    }
    
    // 转换数据格式以匹配后端期望
    const contestData = {
      ...createForm.value,
      start_time: formatDateTime(createForm.value.start_time),
      end_time: formatDateTime(createForm.value.end_time),
      duration: `${createForm.value.duration}m`, // 将数字转换为字符串格式，如 "90m"
      rule: createForm.value.rules // 字段名映射
    }
    
    await adminApi.createContest(contestData)
    alert('竞赛创建成功！')
    showCreateModal.value = false
    resetCreateForm()
    await loadContests()
  } catch (error) {
    console.error('创建竞赛失败:', error)
    alert('创建竞赛失败，请重试')
  } finally {
    loading.value = false
  }
}

const resetCreateForm = () => {
  createForm.value = {
    title: '',
    description: '',
    type: 'individual',
    start_time: '',
    end_time: '',
    duration: 90,
    max_participants: undefined,
    is_public: true,
    rules: ''
  }
}

const editContest = (contest: any) => {
  console.log('编辑竞赛:', contest)
  // 这里可以跳转到编辑竞赛页面
}

const viewContest = (contest: any) => {
  router.push(`/contests/${contest.id}`)
}

const deleteContest = async (contest: any) => {
  if (confirm(`确定要删除竞赛 "${contest.title}" 吗？此操作不可撤销！`)) {
    try {
      loading.value = true
      await adminApi.deleteContest(contest.id)
      alert('竞赛删除成功！')
      await loadContests()
    } catch (error) {
      console.error('删除竞赛失败:', error)
      alert('删除竞赛失败，请重试')
    } finally {
      loading.value = false
    }
  }
}

onMounted(() => {
  loadContests()
})
</script>

